package com.sketch.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.commom.utils.ConversionUtils;
import com.sketch.entity.DramaSeries;
import com.sketch.entity.SketchInfo;
import com.sketch.entity.TheaterInfo;
import com.sketch.entity.dto.DramaSeriesDTO;
import com.sketch.entity.dto.SketchInfoDTO;
import com.sketch.entity.dto.TheaterInfoDTO;
import com.sketch.mapper.TheaterInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Service
public class TheaterInfoService {
    @Resource
    private TheaterInfoMapper theaterMapper;
    public Result addOrUpdate(TheaterInfoDTO theaterInfoDTO){
        if(ObjectUtils.isEmpty(theaterInfoDTO.getId())){
            TheaterInfo theaterInfo = new TheaterInfo();
            BeanUtils.copyProperties(theaterInfoDTO,theaterInfo);
            theaterMapper.insert(theaterInfo);
        }else {
            TheaterInfo theaterInfo = new TheaterInfo();
            BeanUtils.copyProperties(theaterInfoDTO,theaterInfo);
            theaterMapper.updateById(theaterInfo);
        }

        return Result.success();
    }
    public Result list( String name, Integer onlineStatus, Integer page, Integer size){
        LambdaQueryWrapper<TheaterInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(name),TheaterInfo::getName, name)
                .eq(!ObjectUtils.isEmpty(onlineStatus),TheaterInfo::getOnlineStatus, onlineStatus);
        Page<TheaterInfo> sketchInfoPage = new Page<>(page, size);
        IPage<TheaterInfo> result = theaterMapper.selectPage(sketchInfoPage, queryWrapper);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<TheaterInfoDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            TheaterInfoDTO theaterInfoDTO = new TheaterInfoDTO();
            BeanUtils.copyProperties(e,theaterInfoDTO);
            list.add(theaterInfoDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id){
        TheaterInfo theaterInfo = theaterMapper.selectById(id);
        TheaterInfoDTO theaterInfoDTO = new TheaterInfoDTO();
        BeanUtils.copyProperties(theaterInfo,theaterInfoDTO);
        return Result.success().withData(theaterInfoDTO);
    }

    public Result delete(Long id){
        theaterMapper.deleteById(id);
        return Result.success();
    }
}
